/**
 * Copyright 2011 Adrian Witas
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.abstractmeta.commandify.core.command.map;

import org.abstractmeta.commandify.Commandify;
import org.abstractmeta.commandify.command.Commands;
import org.abstractmeta.commandify.core.AbstractCommandify;
import org.abstractmeta.commandify.core.command.AbstractCommand;
import org.abstractmeta.commandify.core.command.AbstractConstructorCommand;
import org.abstractmeta.commandify.core.command.AbstractMethodCommand;
import org.abstractmeta.commandify.core.command.util.CommandifyUtil;
import org.abstractmeta.reflectify.Reflectify;

import java.util.*;

/**
 * Represents a navigableMap which implements commandify.
 * This enables all mutable navigableMap method calls being encapsulated as method command.
 * If either k or value implement commandify then as long as it is possible to build cloned version by commandify, it will be used
 * instead of actual k or v instance value.
 *
 * @author Adrian Witas
 */
public class CommandifyNavigableMap<K, V> extends AbstractCommandify<NavigableMap<K, V>> implements NavigableMap<K, V>, Commandify<NavigableMap<K, V>> {

    private final NavigableMap<K, V> navigableMap;

    public CommandifyNavigableMap(NavigableMap<K, V> navigableMap) {
        super(NavigableMap.class, new MapCommands<NavigableMap<K, V>>(NavigableMap.class));
        this.navigableMap = navigableMap;
        addConstructorCommand();
        commandifyExistingMapItems();
    }


    protected void commandifyExistingMapItems() {
        Commands<NavigableMap<K, V>> commands = getCommands();
        for (Entry<K, V> entry : navigableMap.entrySet()) {
            commands.add(getPutCommand(entry.getKey(), entry.getValue()));
        }
    }

    protected void addConstructorCommand() {
        getCommands().setConstructorCommand(new AbstractConstructorCommand<NavigableMap<K, V>>() {
            @Override
            public NavigableMap<K, V> execute() {
                return new TreeMap<K, V>();
            }
        });


    }

    @Override
    public int size() {
        return navigableMap.size();
    }

    @Override
    public boolean isEmpty() {
        return navigableMap.isEmpty();
    }

    @Override
    public boolean containsKey(Object o) {
        return navigableMap.containsKey(o);
    }

    @Override
    public boolean containsValue(Object o) {
        return navigableMap.containsValue(o);
    }

    @Override
    public V get(Object o) {
        return navigableMap.get(o);
    }

    @Override
    public V put(K k, V v) {
        getCommands().add(getPutCommand(k, v));
        CommandifyUtil.associateAccessor(v, this, k);
        return navigableMap.put(k, v);
    }

    protected AbstractMethodCommand<NavigableMap<K, V>> getPutCommand(final K k, final V v) {
        return new AbstractMethodCommand<NavigableMap<K, V>>("put", new Class[]{k.getClass(), v.getClass()}, new Object[]{k, v}) {

            @Override
            public void execute(NavigableMap<K, V> instance) {
                instance.put(getCommandValue(k), getCommandValue(v));
            }

        };
    }


    @Override
    public V remove(final Object o) {
        getCommands().add(new AbstractMethodCommand<NavigableMap<K, V>>("remove", new Class[]{o.getClass()}, new Object[]{o}) {
            @Override
            public void execute(NavigableMap<K, V> instance) {
                instance.remove(o);
            }
        });
        V result = navigableMap.remove(o);
        CommandifyUtil.dissociateAccessor(result);
        return result;
    }

    @Override
    public void putAll(Map<? extends K, ? extends V> map) {
        for (K key : map.keySet()) {
            this.navigableMap.put(key, map.get(key));
        }
    }

    @Override
    public void clear() {
        getCommands().add(new AbstractMethodCommand<NavigableMap<K, V>>("clear") {
            @Override
            public void execute(NavigableMap<K, V> instance) {
                instance.clear();
            }
        });
        CommandifyUtil.dissociateAccessor(navigableMap.values());
        navigableMap.clear();
    }

    @Override
    public Comparator<? super K> comparator() {
        return navigableMap.comparator();
    }

    @Override
    public Entry<K, V> lowerEntry(K k) {
        return navigableMap.lowerEntry(k);
    }

    @Override
    public K lowerKey(K k) {
        return navigableMap.lowerKey(k);
    }

    @Override
    public Entry<K, V> floorEntry(K k) {
        return navigableMap.floorEntry(k);
    }

    @Override
    public K floorKey(K k) {
        return navigableMap.floorKey(k);
    }

    @Override
    public Entry<K, V> ceilingEntry(K k) {
        return navigableMap.ceilingEntry(k);
    }

    @Override
    public K ceilingKey(K k) {
        return navigableMap.ceilingKey(k);
    }

    @Override
    public Entry<K, V> higherEntry(K k) {
        return navigableMap.higherEntry(k);
    }

    @Override
    public K higherKey(K k) {
        return navigableMap.higherKey(k);
    }

    @Override
    public Entry<K, V> firstEntry() {
        return navigableMap.firstEntry();
    }

    @Override
    public Entry<K, V> lastEntry() {
        return navigableMap.lastEntry();
    }

    @Override
    @SuppressWarnings("unchecked")
    public Entry<K, V> pollFirstEntry() {
        AbstractCommand command = new AbstractMethodCommand<NavigableMap<K, V>>("pollFirstEntry") {
            @Override
            public void execute(NavigableMap<K, V> instance) {
                instance.pollFirstEntry();
            }

        };
        getCommands().add(command);
        Entry<K, V> result = navigableMap.pollFirstEntry();
        if (result != null) {
            CommandifyUtil.dissociateAccessor(result.getValue());
        }
        return result;
    }

    @Override
    @SuppressWarnings("unchecked")
    public Entry<K, V> pollLastEntry() {
        AbstractCommand command = new AbstractMethodCommand<NavigableMap<K, V>>("pollLastEntry") {
            @Override
            public void execute(NavigableMap<K, V> instance) {
                instance.pollLastEntry();
            }
        };
        getCommands().add(command);
        Entry<K, V> result = navigableMap.pollLastEntry();
        if (result != null) {
            CommandifyUtil.dissociateAccessor(result.getValue());
        }
        return result;
    }

    @Override
    public NavigableMap<K, V> descendingMap() {
        return navigableMap.descendingMap();
    }

    @Override
    public NavigableSet<K> navigableKeySet() {
        return navigableMap.navigableKeySet();
    }

    @Override
    public NavigableSet<K> descendingKeySet() {
        return navigableMap.descendingKeySet();
    }

    @Override
    public NavigableMap<K, V> subMap(K k, boolean b, K k2, boolean b2) {
        return navigableMap.subMap(k, b, k2, b2);
    }

    @Override
    public NavigableMap<K, V> headMap(K k, boolean b) {
        return navigableMap.headMap(k, b);
    }

    @Override
    public NavigableMap<K, V> tailMap(K k, boolean b) {
        return navigableMap.tailMap(k, b);
    }

    @Override
    public SortedMap<K, V> subMap(K k, K k2) {
        return navigableMap.subMap(k, k2);
    }

    @Override
    public SortedMap<K, V> headMap(K k) {
        return navigableMap.headMap(k);
    }

    @Override
    public SortedMap<K, V> tailMap(K k) {
        return navigableMap.tailMap(k);
    }


    @Override
    public K firstKey() {
        return navigableMap.firstKey();
    }

    @Override
    public K lastKey() {
        return navigableMap.lastKey();
    }

    @Override
    public Set<K> keySet() {
        return navigableMap.keySet();
    }

    @Override
    public Collection<V> values() {
        return navigableMap.values();
    }

    @Override
    public Set<Entry<K, V>> entrySet() {
        return navigableMap.entrySet();
    }


}
